package com.app.teaware.statis;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import com.app.teaware.R;
import com.app.teaware.basis.BasisFragment;
import com.app.teaware.basis.Constants;
import com.app.teaware.bean.BaseListData;
import com.app.teaware.bean.BaseListDataListBean;
import com.app.teaware.bean.StatisChartInfoBean;
import com.app.teaware.bean.StatisFireRiskBean;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.OnClick;
import my.LogUtil;
import my.StringUtil;
import my.http.MyHttpListener;
import my.http.MyHttpUtils;
import my.http.MyParams;

/**
 * 统计分析-警情统计
 */
public class FireAlarmStatisFragment extends BasisFragment {


    @BindView(R.id.chart_bar)
    BarChart chart_bar;

//    String type_current = "month";

    @BindView(R.id.text_month)
    TextView text_month;
    @BindView(R.id.text_season)
    TextView text_season;
    @BindView(R.id.text_year)
    TextView text_year;
    StatisChartInfoBean mStatisChartInfoBean;

    @BindView(R.id.chart_line_type)
    LineChart chart_line_type;
    StatisChartInfoBean mStatisChartInfoBeanArea;
    @BindView(R.id.text_month_type)
    TextView text_month_type;
    @BindView(R.id.text_season_type)
    TextView text_season_type;
    @BindView(R.id.text_year_type)
    TextView text_year_type;
    String filtType_total = "";
    BaseListData filtData_total;

    @BindView(R.id.chart_bar_num)
    HorizontalBarChart chart_bar_num;
    StatisChartInfoBean mStatisChartInfoBeanActionNum;
    @BindView(R.id.text_month_num)
    TextView text_month_num;
    @BindView(R.id.text_season_num)
    TextView text_season_num;
    @BindView(R.id.text_year_num)
    TextView text_year_num;
    String filtType_num = "";
    BaseListData filtData_num;
    BaseListData filtDataType_num;
    @BindView(R.id.text_type_num)
    TextView text_type_num;

    private final static String TYPE_MONTH = "month", TYPE_SEASON = "season", TYPE_YEAR = "year";
    String filtType = "";
    BaseListData filtData;

    public static FireAlarmStatisFragment newInstance() {
        FireAlarmStatisFragment fragment = new FireAlarmStatisFragment();
        return fragment;
    }

    @Override
    public void initViews() {
//        super.initViews();
        setContentView(R.layout.statis_firealarm_fragment);


    }

    @Override
    public void initData(Bundle savedInstanceState) {
//        type_current =  "month";
//        text_month.setSelected(true);
//        filtType_total = TYPE_MONTH;

        getList();
        getListLine();
        onFiltTypeChoosed_num();
        getActionNumForBar();
    }


    private void getList() {

        MyParams params = new MyParams();
        showProgress();
//        params.put("type", type_current);
        if (!TextUtils.isEmpty(filtType_total) && filtData_total != null) {
            params.put("type", filtType_total);
            params.put("length", filtData_total.getId());
        }
        MyHttpUtils.post(Constants.URL_STATIS_FIRALARM, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        StatisFireRiskBean statisFireRiskBean = (StatisFireRiskBean) result;
                        mStatisChartInfoBean = statisFireRiskBean.getChartInfoBeanBar();
                        mStatisChartInfoBean.setChartBar(mContext, chart_bar, 0);
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        super.onFailure(httpWhat, result);
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        dismissProgress();
                    }
                },
                0, StatisFireRiskBean.class);

    }


    //    @OnClick({R.id.text_month,R.id.text_season,R.id.text_year})
//    void onTimeTabClick(View view){
//        String tag = (String) view.getTag();
//        if(tag.equals(type_current))return;
//        text_month.setSelected(false);
//        text_season.setSelected(false);
//        text_year.setSelected(false);
//        view.setSelected(true);
//        type_current = tag;
//                getList();
//    }
    @OnClick(R.id.text_month)
    void onMonthClick_TOTAL() {
        Calendar calendar = Calendar.getInstance();
        int monthValue = calendar.get(Calendar.MONTH) + 1;
        BaseListDataListBean listBean = new BaseListDataListBean();
        BaseListData baseListData;
        for (int i = 0; i < monthValue; i++) {
            baseListData = new BaseListData(i + 1 + "", getMonthName(i + 1));
            listBean.getList().add(baseListData);
        }
        final String[] items = listBean.getListStringGroup();
        BaseListData dataCurrent = null;
        if (filtType_total == TYPE_MONTH) {
            dataCurrent = filtData_total;
        }
        if (dataCurrent == null) {
            dataCurrent = new BaseListData(monthValue + "", getMonthName(monthValue));
//            LogUtil.i("dataCurrent--->"+dataCurrent.getName()+dataCurrent.getId());
        }
        new QMUIDialog.CheckableDialogBuilder(mContext)
                .setTitle("请选择月份")
                .setCheckedIndex(listBean.getCheckedPosition(dataCurrent))
                .addItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        filtType_total = TYPE_MONTH;
                        filtData_total = listBean.getList().get(which);
                        setFiltTypeView_total();
                        getList();
                        dialog.dismiss();
                    }
                })
                .create().show();
    }


    void setFiltTypeView_total() {
        text_month.setSelected(false);
        text_season.setSelected(false);
        text_year.setSelected(false);
        if (!TextUtils.isEmpty(filtType_total)) {
            switch (filtType_total) {
                case TYPE_MONTH:
                    text_month.setSelected(true);
                    break;
                case TYPE_SEASON:
                    text_season.setSelected(true);
                    break;
                case TYPE_YEAR:
                    text_year.setSelected(true);
                    break;
            }
        }
    }

    @OnClick(R.id.text_season)
    void onSeasonClickTotal() {
        Calendar calendar = Calendar.getInstance();
        int monthValue = calendar.get(Calendar.MONTH) + 1;
        int seaonCurrent = monthValue % 3 == 0 ? monthValue / 3 : (monthValue / 3 + 1);
        LogUtil.i("seaonCurrent--->" + seaonCurrent);
        BaseListDataListBean listBean = new BaseListDataListBean();
        BaseListData baseListData;

        for (int i = 0; i < seaonCurrent; i++) {
            baseListData = new BaseListData(i + 1 + "", "第" + StringUtil.getNumHanzi(i + 1) + "季度");
            listBean.getList().add(baseListData);
        }

        final String[] items = listBean.getListStringGroup();
        BaseListData dataCurrent = null;
        if (filtType_total == TYPE_SEASON) {
            dataCurrent = filtData_total;
        }

        if (dataCurrent == null) {
            dataCurrent = new BaseListData(seaonCurrent + "", "第" + StringUtil.getNumHanzi(seaonCurrent) + "季度");
        }
        new QMUIDialog.CheckableDialogBuilder(mContext)
                .setTitle("请选择季度")
                .setCheckedIndex(listBean.getCheckedPosition(dataCurrent))
                .addItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        filtType_total = TYPE_SEASON;
                        filtData_total = listBean.getList().get(which);
                        setFiltTypeView_total();
                        getList();
                        dialog.dismiss();
                    }
                })
                .create().show();
    }

    @OnClick(R.id.text_year)
    void onYearClickTotal() {
        filtData_num = new BaseListData("", "");
        filtType_total= TYPE_YEAR;
        setFiltTypeView_total();
        getList();
    }


    private void getListLine() {

        MyParams params = new MyParams();
        showProgress();
//        params.put("type", type_current);
        if (!TextUtils.isEmpty(filtType) && filtData != null) {
            params.put("type", filtType);
            params.put("length", filtData.getId());
        }

        MyHttpUtils.post(Constants.URL_STATIS_GET_FIR_BY_AREA, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        StatisFireRiskBean statisFireRiskBean = (StatisFireRiskBean) result;
                        mStatisChartInfoBeanArea = statisFireRiskBean.getChartInfoBeanArea();
                        mStatisChartInfoBeanArea.setChartLine(mContext, chart_line_type);
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        super.onFailure(httpWhat, result);
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        dismissProgress();
                    }
                },
                0, StatisFireRiskBean.class);

    }

    @OnClick(R.id.text_month_type)
    void onMonthClickArea() {
        Calendar calendar = Calendar.getInstance();
        int monthValue = calendar.get(Calendar.MONTH) + 1;
        BaseListDataListBean listBean = new BaseListDataListBean();
        BaseListData baseListData;
        for (int i = 0; i < monthValue; i++) {
            baseListData = new BaseListData(i + 1 + "", getMonthName(i + 1));
            listBean.getList().add(baseListData);
        }
        final String[] items = listBean.getListStringGroup();
        BaseListData dataCurrent = null;
        if (filtType == TYPE_MONTH) {
            dataCurrent = filtData;
        }
        if (dataCurrent == null) {
            dataCurrent = new BaseListData(monthValue + "", getMonthName(monthValue));
//            LogUtil.i("dataCurrent--->"+dataCurrent.getName()+dataCurrent.getId());
        }
        new QMUIDialog.CheckableDialogBuilder(mContext)
                .setTitle("请选择月份")
                .setCheckedIndex(listBean.getCheckedPosition(dataCurrent))
                .addItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        filtType = TYPE_MONTH;
                        filtData = listBean.getList().get(which);
                        setFiltTypeView();
                        getListLine();
                        dialog.dismiss();
                    }
                })
                .create().show();
    }

    private String getMonthName(int monthNum) {
        String result = "";
        switch (monthNum) {
            case 1:
                result = "一月";
                break;
            case 2:
                result = "二月";
                break;
            case 3:
                result = "三月";
                break;
            case 4:
                result = "四月";
                break;
            case 5:
                result = "五月";
                break;
            case 6:
                result = "六月";
                break;
            case 7:
                result = "七月";
                break;
            case 8:
                result = "八月";
                break;
            case 9:
                result = "九月";
                break;
            case 10:
                result = "十月";
                break;
            case 11:
                result = "十一月";
                break;
            case 12:
                result = "十二月";
                break;

        }
        return result;
    }

    void setFiltTypeView() {
        text_month_type.setSelected(false);
        text_season_type.setSelected(false);
        text_year_type.setSelected(false);
        if (!TextUtils.isEmpty(filtType)) {
            switch (filtType) {
                case TYPE_MONTH:
                    text_month_type.setSelected(true);
                    break;
                case TYPE_SEASON:
                    text_season_type.setSelected(true);
                    break;
                case TYPE_YEAR:
                    text_year_type.setSelected(true);
                    break;
            }
        }
    }

    @OnClick(R.id.text_season_type)
    void onSeasonClickArea() {
        Calendar calendar = Calendar.getInstance();
        int monthValue = calendar.get(Calendar.MONTH) + 1;
        int seaonCurrent = monthValue % 3 == 0 ? monthValue / 3 : (monthValue / 3 + 1);
        LogUtil.i("seaonCurrent--->" + seaonCurrent);
        BaseListDataListBean listBean = new BaseListDataListBean();
        BaseListData baseListData;

        for (int i = 0; i < seaonCurrent; i++) {
            baseListData = new BaseListData(i + 1 + "", "第" + StringUtil.getNumHanzi(i + 1) + "季度");
            listBean.getList().add(baseListData);
        }

        final String[] items = listBean.getListStringGroup();
        BaseListData dataCurrent = null;
        if (filtType == TYPE_SEASON) {
            dataCurrent = filtData;
        }

        if (dataCurrent == null) {
            dataCurrent = new BaseListData(seaonCurrent + "", "第" + StringUtil.getNumHanzi(seaonCurrent) + "季度");
        }
        new QMUIDialog.CheckableDialogBuilder(mContext)
                .setTitle("请选择季度")
                .setCheckedIndex(listBean.getCheckedPosition(dataCurrent))
                .addItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        filtType = TYPE_SEASON;
                        filtData = listBean.getList().get(which);
                        setFiltTypeView();
                        getListLine();
                        dialog.dismiss();
                    }
                })
                .create().show();
    }

    @OnClick(R.id.text_year_type)
    void onYearClickArea() {
        filtData = new BaseListData("", "");
        filtType = TYPE_YEAR;
        setFiltTypeView();
        getListLine();

//        Calendar calendar = Calendar.getInstance();
//        int yearCurrent = calendar.get(Calendar.YEAR);
//        Calendar calendarBefor = Calendar.getInstance();
//        calendarBefor.add(Calendar.YEAR,-01);
//        LogUtil.i("onYearClickArea-yearCurrent-->"+yearCurrent);
//        //时间选择器
//        TimePickerView pvTime = new TimePickerBuilder(mContext, new OnTimeSelectListener() {
//            @Override
//            public void onTimeSelect(Date date, View v) {
//                calendar.setTime(date);
//                filtData = new BaseListData(calendar.get(Calendar.YEAR)+"","");
//                filtType = TYPE_YEAR;
//                setFiltTypeView();
//                getListLine();
//            }
//        })
//                .setRangDate(calendarBefor,calendar)
//                .setType(new boolean[]{true, false, false, false, false, false})// 默认全部显示
//                .build();
//        pvTime.show();

    }


    private void getActionNumForBar() {

        MyParams params = new MyParams();
        showProgress();
//        params.put("type", type_current);
        if (!TextUtils.isEmpty(filtType_num) && filtType_num != null) {
            params.put("type", filtType_num);
            params.put("length", filtData_num.getId());
        }
        if (filtDataType_num != null) {
            params.put("top5", filtDataType_num.getId());
        }

        MyHttpUtils.post(Constants.URL_STATIS_GET_POLICE_TOTAL, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
//                        StatisFireRiskBean statisFireRiskBean = (StatisFireRiskBean) result;
//                        mStatisChartInfoBeanArea = statisFireRiskBean.getChartInfoBeanArea();
//                        mStatisChartInfoBeanArea.setChartLine(mContext,chart_line_type);
                        StatisFireRiskBean statisFireRiskBean = (StatisFireRiskBean) result;
                        mStatisChartInfoBean = statisFireRiskBean.getChartInfoBeanBar();
                        mStatisChartInfoBean.reverseData();
                        float angel = 0;
//                        if(filtDataType_num.getName().equals("全部")){
//                            angel = -90;
//                        }else {
//                            angel = -30;
//                        }
                        mStatisChartInfoBean.setChartBarHor(mContext, chart_bar_num, angel);
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        super.onFailure(httpWhat, result);
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        dismissProgress();
                    }
                },
                0, StatisFireRiskBean.class);

    }

    @OnClick(R.id.text_month_num)
    void onMonthClickNum() {
        Calendar calendar = Calendar.getInstance();
        int monthValue = calendar.get(Calendar.MONTH) + 1;
        BaseListDataListBean listBean = new BaseListDataListBean();
        BaseListData baseListData;
        for (int i = 0; i < monthValue; i++) {
            baseListData = new BaseListData(i + 1 + "", getMonthName(i + 1));
            listBean.getList().add(baseListData);
        }
        final String[] items = listBean.getListStringGroup();
        BaseListData dataCurrent = null;
        if (filtType_num == TYPE_MONTH) {
            dataCurrent = filtData_num;
        }
        if (dataCurrent == null) {
            dataCurrent = new BaseListData(monthValue + "", getMonthName(monthValue));
//            LogUtil.i("dataCurrent--->"+dataCurrent.getName()+dataCurrent.getId());
        }
        new QMUIDialog.CheckableDialogBuilder(mContext)
                .setTitle("请选择月份")
                .setCheckedIndex(listBean.getCheckedPosition(dataCurrent))
                .addItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        filtType_num = TYPE_MONTH;
                        filtData_num = listBean.getList().get(which);
                        setFiltTypeView_num();
                        getActionNumForBar();
                        dialog.dismiss();
                    }
                })
                .create().show();
    }


    void setFiltTypeView_num() {
        text_month_num.setSelected(false);
        text_season_num.setSelected(false);
        text_year_num.setSelected(false);
        if (!TextUtils.isEmpty(filtType_num)) {
            switch (filtType_num) {
                case TYPE_MONTH:
                    text_month_num.setSelected(true);
                    break;
                case TYPE_SEASON:
                    text_season_num.setSelected(true);
                    break;
                case TYPE_YEAR:
                    text_year_num.setSelected(true);
                    break;
            }
        }
    }

    @OnClick(R.id.text_season_num)
    void onSeasonClickNum() {
        Calendar calendar = Calendar.getInstance();
        int monthValue = calendar.get(Calendar.MONTH) + 1;
        int seaonCurrent = monthValue % 3 == 0 ? monthValue / 3 : (monthValue / 3 + 1);
        LogUtil.i("seaonCurrent--->" + seaonCurrent);
        BaseListDataListBean listBean = new BaseListDataListBean();
        BaseListData baseListData;

        for (int i = 0; i < seaonCurrent; i++) {
            baseListData = new BaseListData(i + 1 + "", "第" + StringUtil.getNumHanzi(i + 1) + "季度");
            listBean.getList().add(baseListData);
        }

        final String[] items = listBean.getListStringGroup();
        BaseListData dataCurrent = null;
        if (filtType_num == TYPE_SEASON) {
            dataCurrent = filtData_num;
        }

        if (dataCurrent == null) {
            dataCurrent = new BaseListData(seaonCurrent + "", "第" + StringUtil.getNumHanzi(seaonCurrent) + "季度");
        }
        new QMUIDialog.CheckableDialogBuilder(mContext)
                .setTitle("请选择季度")
                .setCheckedIndex(listBean.getCheckedPosition(dataCurrent))
                .addItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        filtType_num = TYPE_SEASON;
                        filtData_num = listBean.getList().get(which);
                        setFiltTypeView_num();
                        getActionNumForBar();
                        dialog.dismiss();
                    }
                })
                .create().show();
    }

    @OnClick(R.id.text_year_num)
    void onYearClickNum() {
        filtData_num = new BaseListData("", "");
        filtType_num = TYPE_YEAR;
        setFiltTypeView_num();
        getActionNumForBar();

//        Calendar calendar = Calendar.getInstance();
//        int yearCurrent = calendar.get(Calendar.YEAR);
//        Calendar calendarBefor = Calendar.getInstance();
//        calendarBefor.add(Calendar.YEAR,-01);
//        LogUtil.i("onYearClickArea-yearCurrent-->"+yearCurrent);
//        //时间选择器
//        TimePickerView pvTime = new TimePickerBuilder(mContext, new OnTimeSelectListener() {
//            @Override
//            public void onTimeSelect(Date date, View v) {
//                calendar.setTime(date);
//                filtData = new BaseListData(calendar.get(Calendar.YEAR)+"","");
//                filtType = TYPE_YEAR;
//                setFiltTypeView();
//                getListLine();
//            }
//        })
//                .setRangDate(calendarBefor,calendar)
//                .setType(new boolean[]{true, false, false, false, false, false})// 默认全部显示
//                .build();
//        pvTime.show();

    }

    private BaseListDataListBean getTypeListBean_num() {
        BaseListDataListBean listBean = new BaseListDataListBean();
        BaseListData baseListData;
        baseListData = new BaseListData("1", "TOP5");
        listBean.getList().add(baseListData);
        baseListData = new BaseListData("0", "全部");
        listBean.getList().add(baseListData);
        return listBean;
    }

    @OnClick(R.id.text_type_num)
    void onTypeClickNum() {
        BaseListDataListBean listBean = getTypeListBean_num();
        final String[] items = listBean.getListStringGroup();
        new QMUIDialog.CheckableDialogBuilder(mContext)
                .setTitle(text_type_num.getHint().toString())
                .setCheckedIndex(listBean.getCheckedPosition(filtDataType_num))
                .addItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        filtDataType_num = listBean.getList().get(which);
                        onFiltTypeChoosed_num();
                        getActionNumForBar();
                        dialog.dismiss();
                    }
                })
                .create().show();
    }

    void onFiltTypeChoosed_num() {
        if (filtDataType_num == null) {
            filtDataType_num = getTypeListBean_num().getList().get(0);
        }
        text_type_num.setText(filtDataType_num.getName());
    }
}
